package com.sys.orders.back.controller;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sys.common.controller.BaseController;
import com.sys.common.pojo.ApiResponse;
import com.sys.common.util.ConstatFinalUtil;
import com.sys.common.util.PageInfoUtil;
import com.sys.users.pojo.AUsers;
import com.sys.users.service.IUsersDbService;

/**
 * 	管理员首页操作
 * 	@author 
 */
@Controller
@RequestMapping(value = "/back/users",produces = "text/html;charset=UTF-8")
public class UsersBackController extends BaseController
{
	@Autowired
	private IUsersDbService usersDbService;
	
	/**
	 * 用户列表
	 * @return
	 * @throws IOException 
	 * @throws ServletException 
	 */
	@RequestMapping("/usersList")
	public String usersList(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
	{
		//this.path = "/back/users/usersList";
		ConstatFinalUtil.SYS_LOGGER.info("--usersList--");
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		
		/*-------自定义搜索条件-------*/
		String userType = request.getParameter("userType");
		if(userType == null)
		{
			userType = "" ;
		}
		condMap.put("userType", userType);
		request.setAttribute("userType", userType);
		
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<AUsers> apiResponse = this.usersDbService.findCondListUsersService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		//this.serverRedirect(request, response, path);
		return "/back/users/usersList";
	}
	
	/**
	 * 打开添加用户页面
	 * @return
	 */
	@RequestMapping("/usersInsert")
	public String usersInsert(HttpServletRequest request,HttpServletResponse response)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--usersInsert--");
		return "/back/users/usersInsert";
	}
	
//	/**
//	 * 添加用户提交操作
//	 * @return
//	 */
//	@RequestMapping("/usersInsertSubmit")
//	@ResponseBody
//	public String usersInsertSubmit(HttpServletRequest request,HttpServletResponse response)
//	{
//		ConstatFinalUtil.SYS_LOGGER.info("--usersInsertSubmit--");
//		String birthdayStr = request.getParameter("birthdayStr");
//		
//		AUsers users = new AUsers();
//		/* 为对象赋值 */
//		users.setBirthday(this.dateFormatUtil.strDate(birthdayStr));
//		/* 为对象赋值 */
//		/* 保存结果 */
//		ApiResponse apiResponse = this.usersDbService.saveOneUsersService(users);
//		ConstatFinalUtil.SYS_LOGGER.info("--用户添加返回结果:{}--",apiResponse.toJSON());
//		return apiResponse.toJSON().toJSONString();
//	}
//	
	/**
	 * 用户查看
	 * @return
	 */
	@RequestMapping("/usersUpdate")
	public String usersUpdate(HttpServletRequest request,HttpServletResponse response)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--usersInfo--");
		String usersId = request.getParameter("usersId");
		/* 准备查询条件 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("usersId", usersId);
		/* 查询数据库 */
		ApiResponse<AUsers> apiResponse = this.usersDbService.findOneUsersService(condMap);
		/* 存储request */
		request.setAttribute("response", apiResponse.toJSON());
		
		String operType = request.getParameter("operType");
		request.setAttribute("operType", operType);
		if("update".equalsIgnoreCase(operType))
		{
			return "/back/users/usersUpdate";
		}
		return "/back/users/usersInfo";
	}
	
	/**
	 * 添加用户提交操作
	 * @return
	 */
	@RequestMapping("/usersUpdateSubmit")
	@ResponseBody
	public String usersUpdateSubmit(HttpServletRequest request,HttpServletResponse response)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--usersInsertSubmit--");
		/* 准备查询条件 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		String usersId = request.getParameter("usersId");
		if(usersId == null)
		{
			usersId = "" ; 
		}
		condMap.put("usersId", usersId);
		/* 查询数据库 */
		ApiResponse<AUsers> apiResponse = this.usersDbService.findOneUsersService(condMap);
		AUsers users = apiResponse.getDataOneJava() ; 
		/* 存储request */
		request.setAttribute("response", apiResponse.toJSON());
		
		String operType = request.getParameter("operType");
		request.setAttribute("operType", operType);
		if("update".equalsIgnoreCase(operType))
		{
			String userPhone = request.getParameter("userPhone");
			String userGender = request.getParameter("userGender");
			String status = request.getParameter("status");
			
			users.setStatus(Byte.valueOf(status));
			users.setUserPhone(userPhone);
			users.setUserGender(userGender);
			
			/* 为对象赋值 */
			users.setUpdateTime(new Date());
			
			ApiResponse<Object> dbResponse = this.usersDbService.updateOneUsersService(users);
			return dbResponse.toJSON().toJSONString();
		}
		return apiResponse.toJSON().toJSONString();
	}
}
